﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CreatePGSQLDataScript
{
    public partial class TaskList : Form
    {
        public TaskList()
        {
            InitializeComponent();
        }
        DataAccess.SqlProvider db;
        public Class.Task tk = new Class.Task();
        private void TaskList_Load(object sender, EventArgs e)
        {
            
            db = new DataAccess.SqlProvider(OisLibrary.DB.Connection);
            //dataGridView1.AutoGenerateColumns = false;

            bindColumn("state", combState);
            bindColumn("class", combClass);
            dateTimePicker1.Value = DateTime.Now.AddDays(-30);
            dateTimePicker2.Value = DateTime.Now;

        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            string sqlText = "select hid,class,title,state,date,detail from tasklist where ";
            sqlText += string.Format(" date between '{0}' and '{1}'", dateTimePicker1.Value.ToString("yyyy-MM-dd"), dateTimePicker2.Value.AddDays(1).ToString("yyyy-MM-dd"));

            if (combClass.Text != "")
            {
                sqlText += string.Format(" and class='{0}'", combClass.Text);
            }
            if (combState.Text != "")
            {
                sqlText += string.Format(" and state='{0}'", combState.Text);
            }
            if (txtContext.Text != "")
            {
                sqlText += string.Format(" and (title  like '%{0}%' or detail like '%{0}%') ", txtContext.Text);
            }

            sqlText += " order by date desc";



            DataTable dt = db.GetDataTable(sqlText);
            dataGridView1.DataSource = dt;

            setColor();
        }
 
        private void bindColumn(string fieldname,ComboBox comb1)
        {
            string sqlText = "select distinct " + fieldname + " as value from tasklist";
            comb1.DisplayMember = "value";
            comb1.ValueMember = "value";
            DataTable dt = db.GetDataTable(sqlText);
            
            DataRow dr = dt.NewRow();
            dr[0] = "";
            dt.Rows.InsertAt(dr, 0);
            comb1.DataSource = dt;
            
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow dr = dataGridView1.CurrentRow;
            tk = new Class.Task();
            tk.hid = dr.Cells[0].Value.ToString();
            tk.TaskClass = dr.Cells[1].Value.ToString();
            tk.title = dr.Cells[2].Value.ToString();
            tk.state = dr.Cells[3].Value.ToString();
            tk.createDate  = DateTime.Parse(dr.Cells[4].Value.ToString());
            tk.detail = dr.Cells[5].Value.ToString();


            TaskView tv = new TaskView(this);
            tv.ShowDialog();

        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            this.tk = null;
            TaskView tv = new TaskView(this);
            tv.ShowDialog();
        }

        private void setColor()
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                Color currColor=Color.White;
                switch (row.Cells[3].Value.ToString())
                {
                    case "完成":
                        currColor = Color.LimeGreen;
                        break;
                    case "更新客户":
                        currColor = Color.Green;
                        break;

                }
                 

                row.Cells[3].Style.BackColor = currColor;

                //if (row.Cells[3].Value.ToString() == "完成")
                //{
                //    row.Cells[3].Style.BackColor = Color.LimeGreen;
                //}
                //if (row.Cells[3].Value.ToString() == "更新客户")
                //{
                //    row.Cells[3].Style.BackColor = Color.Green;
                //}
            }
        }
    }
}
